package com.cyh.io;

import java.io.*;
import java.util.Properties;

public class StreamDemo01 {
    public static void main(String[] args) throws Exception {
        //stream_readFile01();
        //stream_readFile02();
        //stream_writeFile01();
        //stream_copyFile01();
        //reader_readFile01();
        //reader_readFile02();
        //writer_readFile01();
        //buffered_readFile01();
        //buffered_writeFile01();
        //buffered_copyFile01();
        //buffered_stream_copyFile01();
        //object_out_stream();
        //object_in_stream();
        //inputstream_reader(); // 字节流转字符流，解决乱码问题 读文件
        //outputstream_writer(); //字节转码， 写
        //print_stream();
        //print_writer();
        properties_read();
        properties_writer();
    }

    public static void stream_readFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        int readData = 0;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readData = fileInputStream.read()) != -1) {
                System.out.println((char)readData);
            }
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void stream_readFile02() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        int readLen = 0;
        byte[] buf = new byte[1024];
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void stream_writeFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        FileOutputStream fileOutputStream = null;
        try {
            //fileOutputStream = new FileOutputStream(filePath); //会覆盖
            fileOutputStream = new FileOutputStream(filePath, true); //不会覆盖， 追加字符串

            //fileOutputStream.write('a');
            String str = "hello world----";
            //fileOutputStream.write(str.getBytes());
            fileOutputStream.write(str.getBytes(), 1, 5);
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void stream_copyFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        String outFilePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\2.txt";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            fileOutputStream = new FileOutputStream(outFilePath);
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, readLen);
            }
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(fileInputStream != null) {
                    fileInputStream.close();
                }
                if(fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void reader_readFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        FileReader fileReader = null;
        int data;
        try {
            fileReader = new FileReader(filePath);
            while ((data = fileReader.read()) != -1) {
                System.out.println((char)data);
            }
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void reader_readFile02() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        FileReader fileReader = null;
        int readLen;
        char[] buf = new char[1024];
        try {
            fileReader = new FileReader(filePath);
            while ((readLen = fileReader.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writer_readFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(filePath, true); //true表示追加
            fileWriter.write('a');
            fileWriter.write(new char[]{'a', 'b', 'c'});
            fileWriter.write(new char[]{'e', 'e', 'e', 'e', 'e'}, 1, 2);
            fileWriter.write("hhh");
            fileWriter.write("wwwwwwwwww", 1, 2);

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(fileWriter != null) {
                    fileWriter.close();
                    //fileWriter.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void buffered_readFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(filePath));
            String line;
            //line = bufferedReader.readLine();// 按行读取文件，null时表示读取完毕
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(bufferedReader != null) {
                    bufferedReader.close(); //里面的FileReader也会关闭
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void buffered_writeFile01() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(filePath, true));
            bufferedWriter.newLine(); //表示另起一行
            bufferedWriter.write("hello, cyh");
            bufferedWriter.newLine();
            bufferedWriter.write("hello, 123");

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if(bufferedWriter != null) {
                    bufferedWriter.close(); //里面的FileWriter也会关闭
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void buffered_copyFile01() {
        String srcFilePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\1.txt";
        String destFilePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\3.txt";
        BufferedReader br = null;
        BufferedWriter bw = null;
        String line;
        try {
            br = new BufferedReader(new FileReader(srcFilePath));
            bw = new BufferedWriter(new FileWriter(destFilePath, true));

            //readLine表示读取一行的内容，没有换行符
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( br != null) {
                    br.close(); //里面的FileReader也会关闭
                }
                if( bw != null) {
                    bw.close(); //里面的FileWriter也会关闭
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void buffered_stream_copyFile01() {
        String srcFilePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\0.jpg";
        String destFilePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\4.jpg";
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = bis.read(buf)) != -1) {
                bos.write(buf, 0, readLen);
            }


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( bis != null) {
                    bis.close(); //里面的FileInputStream也会关闭
                }
                if( bos != null) {
                    bos.close(); //里面的FileOutputStream也会关闭
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void object_out_stream() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\5.data";
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(filePath));

            oos.writeInt(100);
            oos.writeBoolean(true);
            oos.writeChar('a');
            oos.writeDouble(1.5);
            oos.writeUTF("陈益洪");
            oos.writeObject(new Dog("旺财", 10));


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void object_in_stream() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\5.data";
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(filePath));

            //读取顺序必须与写入数据一样。
            System.out.println(ois.readInt());
            System.out.println(ois.readBoolean());
            System.out.println(ois.readChar());
            System.out.println(ois.readDouble());
            System.out.println(ois.readUTF());
            System.out.println(ois.readObject());

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void inputstream_reader() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\6.txt";
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(new FileInputStream(filePath), "utf-8");
            br = new BufferedReader(isr);

            String s = br.readLine();
            System.out.println(s);


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void outputstream_writer() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\6.txt";
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter(new FileOutputStream(filePath, true), "utf-8");
            osw.write("hi, 甘");


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
            try {
                if( osw != null) {
                    osw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void print_stream() {
        PrintStream out = null;
        try {
            out = System.out;
            out.println("hello world");
            out.write("hi, 陈益洪".getBytes());

            //System.setOut(new PrintStream("D:\\1.txt")); 修改打印位置
            //System.out.println("hello"); 有上面这一句，就会输出的上面指定的文件中。

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {

                if( out != null) {
                    out.close();
                }

        }
    }

    public static void print_writer() {
        PrintWriter printWriter = null;
        try {
            printWriter  = new PrintWriter(System.out);
            //printWriter = new PrintWriter(new FileWriter("e:\\2.txt"));  修改打印的位置
            printWriter.println("hello world");
            printWriter.write("hi, 陈益洪");

            //System.setOut(new PrintStream("D:\\1.txt")); 修改打印位置
            //System.out.println("hello"); 有上面这一句，就会输出的上面指定的文件中。

        } catch (Exception e ) {
            e.printStackTrace();
        } finally {

            if( printWriter != null) {
                printWriter.close();
            }

        }
    }

    public static void properties_read() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\mysql.properties";
        Properties properties = null;
        try {
            properties = new Properties();
            properties.load(new FileReader(filePath));
            properties.list(System.out); //把k-v显示控制台
            String user = properties.getProperty("user");
            String pwd = properties.getProperty("pwd");
            System.out.println("用户：" + user + ", 密码：" + pwd);


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
        }
    }


    public static void properties_writer() {
        String filePath  = "D:\\cyh\\work_java\\TestNioOrIo\\src\\com\\cyh\\io\\mysql.properties";
        Properties properties = null;
        try {
            properties = new Properties();
            properties.setProperty("charset", "utf-8"); //可以写没有的key
            properties.setProperty("user", "aaa");
            properties.setProperty("pwd", "bbb");
            properties.store(new FileOutputStream(filePath), "注释内容， 在文件的最上面会有一行注释");


        } catch (Exception e ) {
            e.printStackTrace();
        } finally {
        }
    }



}
